import { Service } from 'egg';
import { Praise } from '../model/Praise';
import { User } from '../model/User';
import { AssuageGrief } from '../model/Assuage_grief';
import { Comment } from '../model/Comment';

class PraiseService extends Service {
    /**
     * 处理点赞（点赞，取消点赞）
     * @param param 
     */
    public async dealWithPraise(param: any) {
        const operateType = Number(param.operateType);//操作类型（1：点赞，2：取消点赞）
        const targetType = Number(param.targetType);//点赞对象类型（1：用户，2：解忧，3：评论）
        const tran = await this.ctx.model.transaction();
        try {
            if (operateType === 1) {
                const result: any = await this.ctx.model.Praise.findOne<Praise>({
                    attributes: ['praise_id', 'is_delete'],
                    where: { user_id: param.user_id, target_id: param.target_id, praise_type: param.targetType },
                    transaction: tran
                });
                if (!result) {
                    const submitData: any = {};
                    submitData.praise_id = this.ctx.helper.genUUID();
                    submitData.user_id = param.user_id;
                    submitData.target_id = param.target_id;
                    submitData.praise_type = param.targetType;
                    submitData.is_delete = '1';
                    this.ctx.model.Praise.create(submitData, { transaction: tran });
                } else {
                    const praise_id = result.praise_id;
                    const is_delete = Number(result.is_delete);
                    if (is_delete === 1) {
                        return;
                    }
                    this.ctx.model.Praise.update({ is_delete: '1' }, { where: { praise_id }, transaction: tran });
                }
                const target_id = param.target_id;
                if (targetType === 1) {
                    const resultUser: any = await this.ctx.model.User.findOne<User>({ attributes: ['user_praise'], where: { user_id: target_id }, transaction: tran });
                    let user_praise = resultUser.user_praise;
                    if (!user_praise) {
                        user_praise = 1;
                    } else {
                        user_praise += 1;
                    }
                    await this.ctx.model.User.update({ user_praise }, { where: { user_id: target_id }, transaction: tran });
                } else if (targetType === 2) {
                    const resultAssuage: any = await this.ctx.model.AssuageGrief.findOne<AssuageGrief>({ attributes: ['assuage_grief_praise'], where: { assuage_grief_id: target_id }, transaction: tran });
                    let assuage_grief_praise = resultAssuage.assuage_grief_praise;
                    if (!assuage_grief_praise) {
                        assuage_grief_praise = 1;
                    } else {
                        assuage_grief_praise += 1;
                    }
                    await this.ctx.model.AssuageGrief.update({ assuage_grief_praise }, { where: { assuage_grief_id: target_id }, transaction: tran });
                } else if (targetType === 3) {
                    const resultComment: any = await this.ctx.model.Comment.findOne<Comment>({ attributes: ['comment_praise'], where: { comment_id: target_id }, transaction: tran });
                    let comment_praise = resultComment.comment_praise;
                    if (!comment_praise) {
                        comment_praise = 1;
                    } else {
                        comment_praise += 1;
                    }
                    await this.ctx.model.Comment.update({ comment_praise }, { where: { comment_id: target_id }, transaction: tran });
                }
            } else if (operateType === 2) {
                await this.ctx.model.Praise.update({ is_delete: '2' }, { where: { user_id: param.user_id, target_id: param.target_id, praise_type: param.targetType }, transaction: tran });
                const target_id = param.target_id;
                if (targetType === 1) {
                    const resultUser: any = await this.ctx.model.User.findOne<User>({ attributes: ['user_praise'], where: { user_id: target_id }, transaction: tran });
                    let user_praise = resultUser.user_praise;
                    user_praise -= 1;
                    if (user_praise < 0) {
                        user_praise = 0;
                    }
                    await this.ctx.model.User.update({ user_praise }, { where: { user_id: target_id }, transaction: tran });
                } else if (targetType === 2) {
                    const resultAssuage: any = await this.ctx.model.AssuageGrief.findOne<AssuageGrief>({ attributes: ['assuage_grief_praise'], where: { assuage_grief_id: target_id }, transaction: tran });
                    let assuage_grief_praise = resultAssuage.assuage_grief_praise;
                    assuage_grief_praise -= 1;
                    if (assuage_grief_praise < 0) {
                        assuage_grief_praise = 0;
                    }
                    await this.ctx.model.AssuageGrief.update({ assuage_grief_praise }, { where: { assuage_grief_id: target_id }, transaction: tran });
                } else if (targetType === 3) {
                    const resultComment: any = await this.ctx.model.Comment.findOne<Comment>({ attributes: ['comment_praise'], where: { comment_id: target_id }, transaction: tran });
                    let comment_praise = resultComment.comment_praise;
                    comment_praise -= 1;
                    if (comment_praise < 0) {
                        comment_praise = 0;
                    }
                    await this.ctx.model.Comment.update({ comment_praise }, { where: { comment_id: target_id }, transaction: tran });
                }
            }
            await tran.commit();
        } catch (e) {
            await tran.rollback();
            this.ctx.logger.error(e);
            throw new Error(JSON.stringify({ code: 2, msg: '操作失败' }));
        }
    }

    /**
     * 判断是否被点赞
     * @param user_id 
     * @param target_id 
     * @param type 
     */
    public async checkIsPraise(user_id: string, target_id: string, type: string) {
        const result = await this.ctx.model.Praise.findOne<Praise>({ attributes: ['praise_id'], where: { user_id, target_id, praise_type: type, is_delete: '1' } });
        if (!result) {
            return false;
        }
        return true;
    }
}
export default PraiseService;